package com.example.aboutwork.nonblocking;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.function.Function;

@Slf4j
public class Nonblocking<A> {

    private final FutureFunc<A> origin;

    public Nonblocking(A a) {
        origin = FutureFunc.unit(a);
    }

    public Nonblocking(FutureFunc<A> origin) {
        this.origin = origin;
    }


    public <B> Nonblocking<B> map(Function<A, B> f) {
        return new Nonblocking<>(FutureFunc.map(origin, f));
    }

    public <B> Nonblocking<B> flatMap(Function<A, FutureFunc<B>> f) {
        return new Nonblocking<>(FutureFunc.flatMap(origin, f));
    }


    public A run(ExecutorService es) throws InterruptedException {
        return FutureFunc.run(es,origin);
    }
}
